package com.jky.trial.common;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Data
@Component
public class Constants {


    public static String nginxVisitUrl;

    public static String thumbnailPath;
    public static String filePath;
    public static String domain;
    public static String root;
    public static String jgAppKey; //极光AppKey
    public static String jgMasterSecret; //极光MasterSecret
    public static String LOGINED_USER = "USER"; //登录用户SESSION KEY

    @Value("${custom.nginx.visitUrl}")
    public void setNginxVisitUrl(String nginxVisitUrl) {
        this.nginxVisitUrl = nginxVisitUrl;
    }
    @Value("${custom.jg.appKey}")
    public void setJgAppKey(String appKey) {
        this.jgAppKey = appKey;
    }
    @Value("${custom.jg.masterSecret}")
    public void setJgMasterSecret(String masterSecret) {
        this.jgMasterSecret = masterSecret;
    }

    @Value("${custom.server.thumbnailPath}")
    public void setThumbnailPath(String thumbnailPath) {
        this.thumbnailPath = thumbnailPath;
    }

    @Value("${custom.server.filePath}")
    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    @Value("${custom.root}")
    public void setRoot(String root) {
        this.root = root;
    }


    /**
     * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址;
     *
     * @param request
     * @return
     */
    public final static String getIP(HttpServletRequest request) {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
//        logger.debug("[IP=======]");
//        logger.debug(request.getRemoteAddr());
//        logger.debug(request.getAttribute("X-Forwarded-For") + "");
//        logger.debug(request.getHeader("X-Forwarded-For") + "");
        return (String) request.getHeader("X-Forwarded-For");
    }

    /**
     * @param number 数字输初始值
     * @param limit  限定长度
     * @return
     */
    public static String generateLimitNumberStr(int number, int limit) {
        return generateLimitNumberStr(number+"",limit);
    }
    public static String generateLimitNumberStr(String number, int limit){
        String  prefix = "";
        if (number.length() == limit) {
            prefix = number + "";
        } else if (number.length() > limit) {
            throw new Error("数字长度超出限定长度!");
        } else {
            for (int i = 0; i < (limit - number.length()); i++) {
                prefix += "0";
            }
            prefix += number;
        }
        return prefix;
    }

}
